<?php
/**
 * Error controller class
 *
 * This class defines all action methods for the controller of any kind of errors.
 */
class ErrorController extends Zend_Controller_Action
{
	/**
	 * Display the error page
	 */
	public function errorAction()
	{
		$this->getResponse()->clearBody();

		$errors = $this->_getParam('error_handler');
		switch ($errors->type) {
			//TODO : voir pourquoi cette ligne ne marche pas
			//case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
			case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
				$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
				$this->view->errorType = 'notFound';
				break;

			default:
				switch (get_class($errors->exception)) {
					case 'App_PageNotFoundException':
						$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
						$this->view->errorType = 'notFound';
						break;

					case 'App_PermissionException':
						$this->getResponse()->setRawHeader('HTTP/1.1 403 Forbidden');
						$this->view->errorType = 'missingPermissions';
						break;

					default:
						$this->getResponse()->setRawHeader('HTTP/1.1 500 Internal Server Error');
						$this->view->errorType = 'applicationError';

						$logger = new Zend_Log();
						$writer = new Zend_Log_Writer_Stream(dirname(APPLICATION_PATH) . '/data/logs/exceptions.log');
						$logger->addWriter($writer);

						$logger->log($errors->exception->getMessage(), Zend_Log::ERR);
						break;
				}
				break;
		}
	}
}
